package configuration

import (
	"fmt"
	"time"
	server "v8s/http-server"
	"v8s/logger"

	"github.com/caarlos0/env/v6"
)

type (
	Properties struct {
		Log              logger.Properties           `envPrefix:"LOG_"`
		V8S              V8SProperties               `envPrefix:"V8S_"`
		ManagementServer server.HttpServerProperties `envPrefix:"MANAGEMENT_"`
		HttpsServer      server.HttpServerProperties `envPrefix:"HTTPS_SERVER_"`
	}

	V8SProperties struct {
		Cluster string               `env:"CLUSTER,notEmpty"`
		Http    HttpClientProperties `envPrefix:"HTTP_CLIENT_"`
	}

	HttpClientProperties struct {
		BaseUrl        string               `env:"BASE_URL,notEmpty"`
		RequestTimeout time.Duration        `env:"REQUEST_TIMEOUT" envDefault:"180s"`
		TLS            server.TLSProperties `envPrefix:"TLS_"`
	}
)

func ReadProperties() *Properties {
	config := &Properties{}

	if err := env.Parse(config); err != nil {
		panic(fmt.Errorf("read config error: %w", err))
	}

	fmt.Printf("config: %+v", config)
	return config
}
